package org.chen.org.controller;


import io.swagger.annotations.Api;
import org.chen.bas.solution.Qing;
import org.chen.bas.util.AjaxResult;
import org.chen.bas.util.PageInfo;
import org.chen.org.query.DepartmentQuery;
import org.chen.org.service.IDepartmentService;
import org.chen.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
@Api(value = "部门管理",description = "部门操作的CRUD")
@RequestMapping("/department")
@RestController
@Qing(name = "部门管理",desc = "部门管理的crud")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    //    @ApiOperation(value = "更新或新增")
    @PutMapping
    @Qing(name = "部门保存",desc = "部门更新和新增")
    public AjaxResult save(@RequestBody Department department) {

        try {
            if (Objects.isNull(department.getId())) {
                service.insert(department);
            } else {
                service.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    // 删 delete
    @DeleteMapping("/{id}")
    @Qing(name = "部门删除",desc = "根据部门id删除部门")
    public AjaxResult delete(@PathVariable("id") Long id) {
        // 有返回值
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查全部 get
    @GetMapping
    @Qing(name = "部门查询",desc = "查询部门")
    public AjaxResult selectList() {
        // 有返回值
        try {
            List<Department> departments = service.selectAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }


    }

    // 查一个 get
    @GetMapping("/{id}")
    @Qing(name = "部门id查询",desc = "根据部门id查询")
    public AjaxResult selectByID(@PathVariable("id") Long id) {
        try {
             Department department = service.selectById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    //分页查询
    @PostMapping()
    @Qing(name = "分页查询",desc = "部门分页查询")
    public AjaxResult query(@RequestBody DepartmentQuery departmentQuery) {
        try {
           PageInfo pageInfo =service.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @PatchMapping
    @Qing(name = "批量删除",desc = "部门的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        // 有返回值
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/super")
    @Qing(name = "查询部门树",desc = "部门树查询")
    public AjaxResult getSuper() {
        try {
            List<Department> department = service.getSuper();
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
